package com.livedocumentation.client.webservices;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONException;
import com.google.gwt.json.client.JSONObject;
import com.livedocumentation.client.model.ProjectClass;
import com.livedocumentation.client.model.Feature;

public class ConvertJsonpToJavaDocumentation {

	public static ProjectClass convert(ProjectClass projectClass, JSONObject obj) {

		projectClass.setClassPackage(ConvertJsonp.convertToString(obj
				.get("class_package")));

		projectClass.setClassName(ConvertJsonp.convertToString(obj
				.get("class_name")));

		projectClass.setFeatureDescription(ConvertJsonp.convertToString(obj
				.get("class_description")));

		projectClass.setSuperclass(ConvertJsonp.convertToString(obj
				.get("super_class")));

		projectClass.setModifiers(ConvertJsonp.convertToString(obj
				.get("class_modifiers")));

		return projectClass;
	}

	public static String getStatus(JSONArray jsonTags) {

		String tagStatus = null;

		for (int i = 0; i < jsonTags.size(); i++) {

			String tag;
			try {
				tag = jsonTags.get(i).toString().replaceAll("@", "");

				tag = tag.replaceAll("\"", "");

				if (Feature.TODO_STATUS.equals(tag)
						|| Feature.INPROGRESS_STATUS.equals(tag)
						|| Feature.BLOCKED_STATUS.equals(tag)) {

					tagStatus = tag;
				}

			} catch (JSONException e) {

				e.printStackTrace();
			}
		}

		return tagStatus;
	}

	public static String getClassType(JSONArray jsonTags) {

		String tagType = null;

		for (int i = 0; i < jsonTags.size(); i++) {

			try {

				String tag = jsonTags.get(i).toString().replaceAll("@", "");

				tag = tag.replaceAll("\"", "");

				if (Feature.FEATURE_TYPE.equals(tag)
						|| Feature.USER_INTERFACE_TYPE.equals(tag)
						|| Feature.ARCHITECTURE_TYPE.equals(tag)
						|| Feature.ENTITY_TYPE.equals(tag)) {

					tagType = tag;
				}

			} catch (JSONException e) {

				e.printStackTrace();
			}
		}

		return tagType;
	}
}
